package controller

import (
	"context"
	pb "gitee.com/micro-services/protocol/area/api"
	log "github.com/sirupsen/logrus"
	"micro.area.client/ao"
	"micro.area.client/plugins"
)

type AreaController struct{}

func (ac *AreaController) AreaList(ctx context.Context, r *pb.AreaListReq, w *pb.AreaListRsp) error {
	log.Printf("request:%+v", r)

	list, err := ao.AreaList(ctx)
	if err != nil {
		log.Printf("error:%+v", err)
		return err
	}

	w.AreaList = list
	return nil
}

func (ac *AreaController) CreateCircle(ctx context.Context, r *pb.APICreateBusinessCircleReq, w *pb.APICreateBusinessCircleRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "CreateCircle")

	rsp, err := ao.CreateCircle(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.area.CreateCircle", md, r, w, rsp, err)
		return err
	}

	w.Msg = rsp.Msg
	w.RetCode = rsp.RetCode

	plugins.TracerFinish(span, "go.micro.api.area.CreateCircle", md, r, w)
	return nil
}

func (ac *AreaController) DeleteCircle(ctx context.Context, r *pb.APIDeleteBusinessCircleReq, w *pb.APIDeleteBusinessCircleRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "DeleteCircle")

	rsp, err := ao.DeleteCircle(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.area.DeleteCircle", md, r, w, rsp, err)
		return err
	}

	w.RetCode = rsp.RetCode
	w.Msg = rsp.Msg

	plugins.TracerFinish(span, "go.micro.api.area.DeleteCircle", md, r, w)
	return nil
}

func (ac *AreaController) BatchCircleInfo(ctx context.Context, r *pb.APIBatchBusinessCircleInfoReq, w *pb.APIBatchBusinessCircleInfoRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "BatchCircleInfo")

	rsp, err := ao.BatchCircleInfo(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.area.BatchCircleInfo", md, r, w, rsp, err)
		return err
	}

	w.BusinessCircleInfo = rsp.BusinessCircleInfo
	plugins.TracerFinish(span, "go.micro.api.area.BatchCircleInfo", md, r, w)
	return nil
}

func (ac *AreaController) SearchCircle(ctx context.Context, r *pb.APISearchBusinessCircleReq, w *pb.APISearchBusinessCircleRsp) error {
	span, md, spanCtx := plugins.TracerContext(ctx, "SearchCircle")

	rsp, err := ao.SearchCircle(spanCtx, r)
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.area.SearchCircle", md, r, w, rsp, err)
		return err
	}

	w.BusinessCircleInfo = rsp.BusinessCircleInfo
	plugins.TracerFinish(span, "go.micro.api.area.SearchCircle", md, r, w)
	return nil
}
